home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / Installer SDK Cornucopia 1.0.2 / Script Examples / Search Proc [insp] Example / FindTargetFile.c next >
Encoding:
Text File  |  1996-09-30  |  12.5 KB  |  322 lines  |  [TEXT/MPS ]

  1. //
  2. //    FindTargetFile.c
  3. //
  4. //        A file search proc for use with the Apple Installer 4.x.
  5. //
  6. //        6/1/94 - original code by Rob "Lunatic" Moore
  7. //        6/8/94 • mark young - ≈.2 through ≈.4 backup copies 
  8. //                    - added code to return different values to installer depending
  9. //                    on whether files were found or not, or if count of found files is invalid
  10. //                    - added stub code to simulate nil found file list handle, and returning
  11. //                    the various possible result codes ( see bug #1148502 )
  12. //                    NOTE:     I noticed that the return value must be kSearchSuccessful for an installer
  13. //                            rule clause ( referencing an 'insp' ) to fire as true.
  14. //                    NOTE:    I noticed that the two non-zero return values ( kFatalSearchError and
  15. //                            kCancelSearchAndInstallation ) have no effect on cancelling installation
  16. //                            when called from within an installer rule clause ( referencing an 'insp' )
  17. //
  18. //        Copyright 1993-1996, Apple Computer, Inc., All Rights Reserved
  19. //
  20.  
  21. #include <Files.h>
  22. #include <GestaltEqu.h>
  23. #include <Memory.h>
  24. #include <OSUtils.h>
  25. #include <Packages.h>
  26. #include <TextUtils.h>
  27.  
  28. #ifndef __ActionHandlerHeader__
  29. #include "ActionHandlerHeader.h"
  30. #endif
  31.  
  32. #ifndef InstallerScript
  33. #include "InstallerScript.h"
  34. #endif
  35.  
  36.  
  37. void DisplaySearchInfo( SearchProcedurePBPtr pSearchProcedurePBPtr, SearchResult pResultCode );
  38. FoundFileArrayHdl MakeFoundFilesArrayHdl(InstallerCallBackUPP pCallBackProcPtr);
  39. OSErr    AddFSSpecToFoundFilesArrayHdl(    FoundFileArrayHdl    pFoundFileArrayHdl,
  40.                                         FSSpec                pFSSpec );
  41. void PrintLine( InstallerCallBackUPP pCallBackProcPtr, Str255 pParam0, Str255 pParam1, Str255 pParam2, Str255 pParam3 );
  42.  
  43. SearchResult FindTargetFile( SearchProcedurePBPtr pSearchProcedurePBPtr )
  44. {
  45.     #define            kMaxNumOfMatches    40
  46.     #define            kOptBufferSize        0x100
  47.  
  48.     SearchResult        result;
  49.     FoundFileArrayHdl    myFoundFileArrayHdl;
  50.  
  51.     long                theSysVersionNum;
  52.  
  53.     OSErr                theErr;
  54.     CInfoPBRec            theFirstSearchCriteria;
  55.     CInfoPBRec            theSecondSearchCriteria;
  56.     CSParam                theCatParamBlock;
  57.     FSSpec                theMatchedFSSpecArray[kMaxNumOfMatches];
  58.     char                theOptionalBuffer[kOptBufferSize];
  59.     short                theByteCtr;
  60.     short                theFoundFSSpecCtr;
  61.     FSSpec                theTempFSSpec;
  62.  
  63. // dummy code to simulate all possibilities of passing back a nil handle to search file list
  64. //myFoundFileArrayHdl = NULL;
  65. //return( kSearchSuccessful );
  66. //return( kFatalSearchError );
  67. //return( kCancelSearchAndInstallation );
  68.  
  69.     myFoundFileArrayHdl = MakeFoundFilesArrayHdl( pSearchProcedurePBPtr->fCallBackProcPtr );
  70.     
  71.     if( myFoundFileArrayHdl != NULL ) {
  72.     
  73.         Gestalt( 'sysv', &theSysVersionNum );
  74.         if( theSysVersionNum >= 0x00000700 ) {
  75.         
  76.             theCatParamBlock.ioCompletion        = NULL;
  77.             theCatParamBlock.ioNamePtr            = NULL;
  78.             theCatParamBlock.ioVRefNum            = pSearchProcedurePBPtr->fTargetVRefNum;
  79.             
  80.             theCatParamBlock.ioMatchPtr            = (FSSpecArrayPtr)theMatchedFSSpecArray;    /* match array */
  81.             theCatParamBlock.ioReqMatchCount    = kMaxNumOfMatches;                            /* maximum allowable matches */
  82.             theCatParamBlock.ioSearchBits        = fsSBFlFndrInfo + fsSBFlAttrib;            /* search criteria selector */
  83.             theCatParamBlock.ioSearchInfo1        = &theFirstSearchCriteria;                    /* search values and range lower bounds */
  84.             theCatParamBlock.ioSearchInfo2        = &theSecondSearchCriteria;                    /* search values and range upper bounds */
  85.             theCatParamBlock.ioSearchTime        = 0;                                        /* length of time to run search */
  86.             theCatParamBlock.ioCatPosition.initialize    = 0;                                /* current position in the catalog */
  87.             theCatParamBlock.ioOptBuffer        = theOptionalBuffer;                        /* optional performance enhancement buffer */
  88.             theCatParamBlock.ioOptBufSize        = kOptBufferSize;                            /* size of buffer pointed to by ioOptBuffer */
  89.         
  90.             theFirstSearchCriteria.hFileInfo.ioFlFndrInfo.fdType        = pSearchProcedurePBPtr->fFileSpecType;            /* An application */
  91.             theFirstSearchCriteria.hFileInfo.ioFlFndrInfo.fdCreator        = pSearchProcedurePBPtr->fFileSpecCreator;        /* TeachText/SimpleText creator */
  92.             theFirstSearchCriteria.hFileInfo.ioFlAttrib                    = 0x00;                /* A File */
  93.     
  94.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdType        = 0xFFFFFFFF;        /*the type of the file*/
  95.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdCreator    = 0xFFFFFFFF;        /*file's creator*/
  96.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdFlags        = 0x0000;            /*flags ex. hasbundle,invisible,locked, etc.*/
  97.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdLocation.h    = 0x0000;            /*file's location in folder*/
  98.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdLocation.v    = 0x0000;            /*file's location in folder*/
  99.             theSecondSearchCriteria.hFileInfo.ioFlFndrInfo.fdFldr        = 0x0000;            /*folder containing file*/
  100.             theSecondSearchCriteria.hFileInfo.ioFlAttrib                = 0x10;                /* Check file/directory bit */
  101.             
  102.             theErr = PBCatSearchSync( &theCatParamBlock );
  103.         
  104.             // Add each found
  105.             for( theFoundFSSpecCtr = 0; theFoundFSSpecCtr < theCatParamBlock.ioActMatchCount; theFoundFSSpecCtr++ ) {
  106.                 theTempFSSpec.vRefNum    = theMatchedFSSpecArray[theFoundFSSpecCtr].vRefNum;
  107.                 theTempFSSpec.parID    = theMatchedFSSpecArray[theFoundFSSpecCtr].parID;
  108.                 
  109.                 for( theByteCtr = 0; theByteCtr <= theMatchedFSSpecArray[theFoundFSSpecCtr].name[0]; theByteCtr++ )
  110.                     theTempFSSpec.name[theByteCtr]    = theMatchedFSSpecArray[theFoundFSSpecCtr].name[theByteCtr];
  111.  
  112.                 AddFSSpecToFoundFilesArrayHdl( myFoundFileArrayHdl,theTempFSSpec );    
  113.             }
  114.         
  115.         }
  116.         else {
  117.         
  118.             // Implement the pre-7.0 search code here
  119.         
  120.         }
  121.             
  122.         
  123.         pSearchProcedurePBPtr->fFoundFilesArray = myFoundFileArrayHdl;
  124.         
  125.         // one or more files were found
  126.         // this causes installation to continue normally, no dlogs are displayed
  127.         if ( theCatParamBlock.ioActMatchCount > 0 )
  128.             result = kSearchSuccessful;
  129.             
  130.         // no files were found
  131.         // this causes dlog "Installer doc is damaged..." to be displayed, cancels installation
  132.         else if ( theCatParamBlock.ioActMatchCount == 0 )
  133.             result = kFatalSearchError;
  134.             
  135.         // illegal file count
  136.         // this causes dlog "Installation was cancelled..." to be displayed, cancels installation
  137.         else result = kCancelSearchAndInstallation;
  138.                         
  139.         DisplaySearchInfo( pSearchProcedurePBPtr, result );
  140.  
  141.     }
  142.     else 
  143.         // this causes dlog "Installer doc is damaged..." to be displayed, cancels installation
  144.         result = kFatalSearchError;
  145.             
  146.     return result;
  147. }
  148.  
  149.  
  150.  
  151. // ***********************************************************************************************
  152. // ******************************** FoundFileArrayHdl routines ***********************************
  153. // ***********************************************************************************************
  154.  
  155. FoundFileArrayHdl MakeFoundFilesArrayHdl( InstallerCallBackUPP    pCallBackProcPtr)
  156. {
  157.     return (FoundFileArrayHdl)INewHandle( pCallBackProcPtr, 0 );
  158. }
  159.  
  160. OSErr    AddFSSpecToFoundFilesArrayHdl(    FoundFileArrayHdl    pFoundFileArrayHdl,
  161.                                         FSSpec                pFSSpec )
  162. {
  163.     FSSpecPtr            newFSSpecPtr;
  164.     OSErr                theErr     = noErr;
  165.     Size                orgSize = GetHandleSize( (Handle)pFoundFileArrayHdl );
  166.     
  167.     if( pFoundFileArrayHdl != NULL ) {
  168.         SetHandleSize( (Handle)pFoundFileArrayHdl, orgSize + sizeof( FoundFileRec ) );
  169.         theErr = MemError();
  170.         
  171.         if( theErr == noErr ) {
  172.             newFSSpecPtr = (FSSpecPtr)(StripAddress(*pFoundFileArrayHdl) + orgSize);
  173.             *newFSSpecPtr = pFSSpec;
  174.         }
  175.     }
  176.     
  177.     return theErr;
  178. }
  179.  
  180.  
  181.  
  182. // -------------------------------------------------------------------------------------------------------------------------------------
  183. // The routines below are for displaying the contents of the parameter block passed to the Setup Function.
  184. // Call DisplayPreferenceInfo to write this information to the Installer Debugger.
  185.  
  186. void MakeFilePath( FSSpec* pFileFSSpec, Str255  pFilePath )
  187. {
  188.     
  189.     CInfoPBRec        cPBRec;
  190.     Str255            directoryName = "";
  191.     short            i;
  192.     OSErr            theErr;
  193.  
  194.     // Get the file name
  195.     for( i=0;i<=pFileFSSpec->name[0];i++)
  196.         pFilePath[i] = pFileFSSpec->name[i];
  197.  
  198.     cPBRec.hFileInfo.ioCompletion = NULL;
  199.     cPBRec.hFileInfo.ioNamePtr = directoryName;
  200.     cPBRec.hFileInfo.ioFDirIndex = -1;
  201.     cPBRec.hFileInfo.ioDirID = pFileFSSpec->parID;
  202.     cPBRec.hFileInfo.ioVRefNum = pFileFSSpec->vRefNum;
  203.     theErr = PBGetCatInfo( &cPBRec, 0 );
  204.  
  205.     // Preappend directoryName
  206.     BlockMove( pFilePath + 1, pFilePath + directoryName[0] + 2, pFilePath[0] );
  207.     for( i=1;i<=directoryName[0];i++)
  208.         pFilePath[i] = directoryName[i];
  209.     pFilePath[i] = ':';
  210.     pFilePath[0] += directoryName[0] + 1;
  211.  
  212.     while ( theErr == noErr && cPBRec.dirInfo.ioDrParID != 1 && pFilePath[0] + directoryName[0] + 1 < 255 ) {
  213.  
  214.     // Preappend directoryName
  215.         cPBRec.hFileInfo.ioDirID = cPBRec.dirInfo.ioDrParID;
  216.         cPBRec.hFileInfo.ioFDirIndex = -1;
  217.         theErr = PBGetCatInfo( &cPBRec, 0 );
  218.  
  219.         if( theErr == noErr ) {
  220.             // Preappend directoryName
  221.             BlockMove( pFilePath + 1, pFilePath + directoryName[0] + 2, pFilePath[0] );
  222.             for( i=1;i<=directoryName[0];i++)
  223.                 pFilePath[i] = directoryName[i];
  224.             pFilePath[i] = ':';
  225.             pFilePath[0] += directoryName[0] + 1;
  226.         }
  227.     }
  228.  
  229. }
  230.  
  231. void PrintLine( InstallerCallBackUPP pCallBackProcPtr, Str255 pParam0, Str255 pParam1, Str255 pParam2, Str255 pParam3 )
  232. {
  233.     long    theResult;
  234.     RegisterScriptAction( pCallBackProcPtr, kDebuggingAction, kGenericDebugActID, pParam0, pParam1, pParam2, pParam3, &theResult );    
  235. }
  236.  
  237. void DisplaySearchInfo( SearchProcedurePBPtr pSearchProcedurePBPtr, SearchResult pResultCode )
  238. {
  239.  
  240. StringPtr                    kBeginCallPart1                    = "\p=>========================== Begin File Search Function Call ====";
  241. StringPtr                    kBeginCallPart3                    = "\p==================================================";
  242.  
  243. StringPtr                    kEndCallPart1                    = "\p-<-------------------------- End File Search Function Call ------ ";
  244. StringPtr                    kEndCallPart3                    = "\p ----------------------------\n";
  245.  
  246. StringPtr                    kResultText                        = "\p       Result Code: ";
  247. StringPtr                    kRefConText                        = "\p            RefCon: ";
  248. StringPtr                    kFileTypeText                    = "\p              Type: ";
  249. StringPtr                    kFileCreatorText                = "\p           Creator: ";
  250. StringPtr                    kFileFileSpecPathText            = "\p    File Spec Path: ";
  251.  
  252. StringPtr                    kFilesFoundTitleText            = "\pFound Files...";
  253. StringPtr                    kNoFilesFoundTitleText            = "\pNo Files Found!";
  254. StringPtr                    kFoundFilePathText                = "\p        Found File:  ";
  255.  
  256. StringPtr                    kTargetFolderPathText            = "\p    Target Folder Path:  ";
  257. StringPtr                    kSystemDiskText                    = "\p      System Disk Name:  ";
  258.  
  259. Str255                        tempNumStr;    
  260. Str255                        tempStr255;    
  261.  
  262. short                        numOfFoundFiles;
  263. short                        foundFileCtr;
  264. FoundFileRec                theFoundFileRec;
  265.  
  266.     // -- Beginning line
  267.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kBeginCallPart1, kBeginCallPart3, "\p", "\p" );
  268.  
  269.     // —— Print File Path from File Spec
  270.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFileFileSpecPathText, pSearchProcedurePBPtr->fFileSpecPath, "\p", "\p" );
  271.     
  272.     // —— Print Type from File Spec
  273.     BlockMove( &(pSearchProcedurePBPtr->fFileSpecType), &(tempNumStr[2]), 4 );
  274.     tempNumStr[1] = '\'';
  275.     tempNumStr[6] = '\'';
  276.     tempNumStr[0] = 6;
  277.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFileTypeText, tempNumStr, "\p", "\p" );
  278.     
  279.     // —— Print Creator from File Spec
  280.     BlockMove( &(pSearchProcedurePBPtr->fFileSpecCreator), &(tempNumStr[2]), 4 );
  281.     tempNumStr[1] = '\'';
  282.     tempNumStr[6] = '\'';
  283.     tempNumStr[0] = 6;
  284.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFileCreatorText, tempNumStr, "\p", "\p" );
  285.     
  286.     // —— Print RefCon
  287.     NumToString( pSearchProcedurePBPtr->fRefCon, tempNumStr );
  288.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kRefConText, tempNumStr, "\p", "\p" );
  289.     
  290.     if( pSearchProcedurePBPtr->fFoundFilesArray != NULL ) {
  291.         numOfFoundFiles = GetHandleSize( (Handle)pSearchProcedurePBPtr->fFoundFilesArray ) / sizeof( FoundFileRec);
  292.         
  293.         if( numOfFoundFiles > 0 )
  294.             PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFilesFoundTitleText, "\p", "\p", "\p" );
  295.         else
  296.             PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kNoFilesFoundTitleText, "\p", "\p", "\p" );
  297.         
  298.         // Print found file paths
  299.         for( foundFileCtr = 0; foundFileCtr < numOfFoundFiles; foundFileCtr++ ) {
  300.             theFoundFileRec = (*(pSearchProcedurePBPtr->fFoundFilesArray))[foundFileCtr];
  301.             MakeFilePath( (FSSpec*)&theFoundFileRec, tempStr255 );
  302.             PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kFoundFilePathText, tempStr255, "\p", "\p" );
  303.         }
  304.     }
  305.  
  306.     // -- Ending line
  307.     NumToString( pResultCode, tempNumStr );
  308.     PrintLine( pSearchProcedurePBPtr->fCallBackProcPtr, kEndCallPart1, kResultText, tempNumStr, kEndCallPart3 );
  309. }
  310.  
  311.  
  312. // Because this include contains functions that
  313. // will be compiled, and we want to make sure
  314. // that our main entry point 'GetRsrcVersion()'
  315. // is placed at the top of the code resource
  316. // that is generated, this include statement
  317. // is put at the end of this file. Even better
  318. // method would be to compile this included file
  319. // as a seperate library and to link that library
  320. // to the GetRsrcVersion() object file.
  321. #include "InstallerCallbackGlue.c"
  322.